<template>
  <div class="app-container">

    <div>
      平台进入帐号：
      <el-input
        v-model="username"
        placeholder="admin"
        style="width:240px"
        :disabled="true"/>
    </div>
    <br/>

    <div>
      <h4>
        修改密码
      </h4>
      <div>
        旧密码*：
        <el-input
          v-model="oldPassword"
          placeholder="旧密码"
          style="width:240px"/>
      </div>
      <br/>
      <div>
        新密码*：
        <el-input
          v-model="newPassword"
          placeholder="新密码"
          style="width:240px"/>
      </div>
      <font style="font-size: 10px">*新密码必须至少包含8个字符，包括大写/小写和数字</font>
      <br/><br/>
      <div>
        新密码确认*：
        <el-input
          v-model="repeatNewPassword"
          placeholder="确认新密码"
          style="width:240px"/>
      </div>
    </div>
    <br/>

    <div>
      <el-button type="primary" @click="resetPassword">立即提交</el-button>
    </div>
    <br/>
    <hr/>
    <br/>

    <div>
      <h4>平台数据清除</h4>
      <div>
        时间：
        <el-date-picker
          v-model="startTime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
        -
        <el-date-picker
          v-model="endTime"
          align="right"
          type="date"
          placeholder="选择日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </div>
    </div>
    <br/>
    <el-button type="primary" @click="deleteData">立即清除</el-button>
  </div>
</template>

<script>
  import axios from "axios";
  import G from "@/components/G";

  export default {
    data() {
      return {

        /*密码*/
        username: 'admin',
        oldPassword: '',
        newPassword: '',
        repeatNewPassword: '',


        /*日期*/
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        startTime: '',
        endTime: '',
      }
    },
    methods: {
      resetPassword() {
        if (this.oldPassword.length < 1) {
          alert('请输入旧密码！');
          return;
        }

        if (this.newPassword.length < 8) {
          alert('新密码长度不足8位！');
          return;
        }
        if ((this.newPassword.match(/([a-z])+/) && this.newPassword.match(/([A-Z])+/) && this.newPassword.match(/([0-9])+/)) === false) {
          alert('新密码需包含大/小写和数字！');
          return;
        }
        if (this.newPassword !== this.repeatNewPassword) {
          alert('两次新密码输入不一致！');
          return;
        }

        let param = '&username=' + this.username
          + '&oldPassword=' + this.oldPassword
          + '&newPassword=' + this.newPassword;
        axios.post(G.host + 'resetPassword', param).then(res => {
          if (res.data.code === -1) {
            alert(res.data.message);
            return;
          }
          alert('密码修改成功');
          this.logout();
        });
      },
      async logout() {
        await this.$store.dispatch('user/logout')
        this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      },

      checkTime(i) {
        if (i < 10) {
          i = "0" + i
        }
        return i;
      },
      deleteData() {
        let startDate = new Date(this.startTime);
        let requestStartTime = startDate.getFullYear() + '-' + this.checkTime(startDate.getMonth() + 1) + '-' + this.checkTime(startDate.getDate());
        let endDate = new Date(this.endTime);
        let requestEndTime = endDate.getFullYear() + '-' + this.checkTime(endDate.getMonth() + 1) + '-' + this.checkTime(endDate.getDate());
        requestStartTime = requestStartTime + " 00:00:00"
        requestEndTime = requestEndTime + " 23:59:59"
        let param = '&startTime=' + requestStartTime
          + '&endTime=' + requestEndTime
        axios.post(G.host + 'deleteOrderByDateRange', param).then(res => {
          this.$notify({
            title: '删除成功',
            //message: this.$createElement('i', {style: 'color: teal'}, res.data.data),
            duration: 2000
          })
        })
      }
    },
    mounted() {
      //初始化当前日期
      this.username = G.username
      this.startTime = ''
      this.endTime = ''
    }
  }
</script>
